import mod_verbs,mod_line
from mod_ai import AIBrain
from items.guns import Shoot,rayPrototype
import mod_actor

class TurretBrain(AIBrain):
    def findOffensiveRangedAbility(self):
        for ability in self.actor.abilities:
            if isinstance(ability,Shoot):
                return ability
        return None
    
    def takeTurn(self):
        actor = self.actor
        # See if we can shoot the player, if so, do it.
        AIBrain.takeTurn(self)
        level,origin = actor.location
        
        lastPlayerLoc = self.memory.lastPlayerLoc
        if lastPlayerLoc:
            playerLevelNum,playerCoords = lastPlayerLoc
            # Should change the inRange call to use the levelMemory.
            if playerLevelNum == level.number and mod_line.inRange(level,origin,playerCoords):
                shootAbility = self.findOffensiveRangedAbility()
                while actor.AP >= shootAbility.calculateAP(actor,None,playerCoords):
                    shootAbility.doIt(actor,None,playerCoords)

turretShootAbility = Shoot(rayPrototype)
turretPrototype = mod_actor.ActorPrototype('TURRET', maxHP=3, maxAP=6, APperTurn=6, brainPrototype=TurretBrain,structural=True)
turretPrototype.abilities.append(turretShootAbility)
